import Vue from "vue";
import axios from "axios";
// Vue.use(axios);
const qs = require('qs');

import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

import {
    Toast
} from 'vant';
Vue.use(Toast);


//$cookies.set('token','4c208dc8-421f-430f-af91-2f846b093c57');//lc陈绍


// $cookies.set('token', '11b23afb-4b65-4b5f-a5ab-fd6bfc4e30bc'); //lc测试服

//$cookies.set('token', '4003fbdd-d5af-4c94-87f2-18367e0049ed');
//$cookies.set('token', '9af8fef2-62db-4e78-a3e0-a4d07101ff02');




axios.defaults.baseURL = process.env.BASE_URL;
axios.defaults.timeout = 30000;
//设置默认请求头
// axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
axios.defaults.headers = {
    "Content-Type": "application/x-www-form-urlencoded",
}

axios.interceptors.request.use(request => {
    Toast.loading({
        mask: true,
        message: '加载中...',
        duration: 0, // 持续展示 toast
        forbidClick: true
    });
    if (request.data && request.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
        request.data = qs.stringify(request.data);
    }

    var token = $cookies.get('token') || localStorage.getItem('token');


    if (token && token != null) {
        request.headers["Authorization"] = 'Bearer ' + token;
    } else {
        //统一设置。全部页面需要token 
        var returnUrl = window.location.href;
        window.location.href = process.env.BASE_URL + "?returnUrl=" + returnUrl;
    }

    return request
}, error => {
    return Promise.reject(error);
})

axios.interceptors.response.use(response => {

    Toast.clear();
    console.warn('closeloading');
    return response.data;
}, error => {
    Toast.clear();
    if (error.response) {
        Toast.fail(error);
    }
    return Promise.reject(error);
})




export default axios;